<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="messageId" />
      <!-- <el-table-column label="应用类型" align="center" prop="appType" :formatter="appTypeFormat" /> -->
      <el-table-column label="标题" align="center" prop="title" />
      <!-- :show-overflow-tooltip="true" -->
      <el-table-column label="文本内容" align="center" prop="content">
        <template slot-scope="scope">
          <span style="cursor: pointer" @click="seeckFun(scope.row)">查看</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="图片" align="center" prop="image">
        <template slot-scope="scope">
          <img :src="requestUrl + scope.row.image" alt="" />
        </template>
      </el-table-column> -->
      <el-table-column label="最后更新时间" align="center" prop="lastModifiedTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改系统消息通知对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">

        <el-form-item label="消息标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入消息标题" />
        </el-form-item>
        <!-- <el-form-item label="消息内容" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item> -->
        <el-form-item label="消息内容" prop="content">
          <div style="border: 1px solid #ccc; width: 625px">
            <Toolbar style="border-bottom: 1px solid #ccc" ref="toolbartag" :editor="editor"
              :defaultConfig="toolbarConfig" :mode="mode" />
            <Editor style="height: 500px; overflow-y: hidden" v-model="form.content" :defaultConfig="editorConfig"
              :mode="mode" @onCreated="onCreated" @onChange="onChange" />
          </div>
        </el-form-item>

        <!-- <el-form-item label="图片" prop="image">
          <el-upload
            :limit="1"
            accept=".jpg, .png"
            :action="upload.url"
            :headers="upload.headers"
            :file-list="upload.fileList"
            :on-success="handleFileSuccess"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看文本內容 -->
    <el-dialog title="文本內容" :visible.sync="opendel" width="750px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="文本內容: ">
          <div v-html="editotext" class="eitd"></div>
        </el-form-item>
        <br />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="opendel = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMessage,
  delMessage,
  addMessage,
  updateMessage,
  uploadimg,
} from "@/api/admin/sysMessage";
import { getToken } from "@/utils/auth";
import "@wangeditor/editor/dist/css/style.css";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";

export default {
  name: "Message",
  components: { Editor, Toolbar },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统消息通知表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opendel: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: { appType: 1 },
      appTypeOptions: [
        { dictValue: "1", dictLabel: "姑往今徕" },
      ],
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/admin/upload/uploadImg/1",
        // 上传的文件列表
        fileList: [],
      },
      requestUrl: this.$img.requestUrl,
      //富文本
      editor: null,
      html: "",
      toolbarConfig: {
        excludeKeys: [
          "fullScreen", //不显示全屏
          "insertVideo", //不显示插入视频
          "uploadVideo",
        ],
      },
      editorConfig: {
        placeholder: "请输入内容...",
        autoFocus: false,
        MENU_CONF: {
          // 所有的菜单配置，都要在 MENU_CONF 属性下
          // 配置上传图片
          uploadImage: {
            customUpload: this.update,
          },
        },
      },
      mode: "default", // or 'simple'
      editotext: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    seeckFun(row) {
      this.editotext = row.content;
      this.opendel = true;
    },
    // 富文本方法
    onCreated(editor) {
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错
      // console.log("uploadImagesssss", this.editor);
    },
    onChange(editor) {
      // this.imghtml = editor.getHtml();
    },
    // 富文本上传
    update(file, insertFn) {
      let formData = new FormData();
      formData.append("file", file);
      uploadimg(formData).then((res) => {
        if (res.code == 200) {
          // 最后插入图片
          insertFn(
            this.requestUrl + res.data.list[0].filePath,
            res.data.list[0].fileId,
            this.requestUrl + res.data.list[0].filePath
          );
        } else {
        }
      });
    },
    /** 查询系统消息通知列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryParams).then((response) => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 类型字典翻译
    appTypeFormat(row, column) {
      return this.selectDictLabel(this.appTypeOptions, row.appType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      // this.form = {};
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.messageId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;

      this.title = "新增系统消息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改系统消息";
      this.form = { ...row };
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.messageId != null) {
            updateMessage(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addMessage(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const messageId = row.messageId;
      this.$confirm("是否确认删除该系统消息?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delMessage(messageId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () { });
    },
    // 文件提交处理
    submitUpload() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.form.image = response.data.list[0].filePath;
      this.msgSuccess(response.msg);
    },
  },
};
</script>
<style>
.eitd img {
  width: 100%;
}

.w-e-bar-item button .title {
  margin: 0 auto 0px 5px !important;
}
</style>
